package com.zone.vehicle.superapp.view.login.setSecret;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.module.account.entity.UserInfoEntity;
import com.zone.vehicle.superapp.databinding.FragmentSetSecretBinding;
import com.zone.vehicle.superapp.model.LoginViewModel;
import com.zone.vehicle.superapp.model.ViewModelFactory;
import com.zone.vehicle.superapp.view.common.base.BaseFragment;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import static com.zone.vehicle.superapp.view.login.LoginFragment.LOGIN_SECRET_FRAGMENT;

public class SetSecretFragment extends BaseFragment {

    private FragmentSetSecretBinding binding;

    private LoginViewModel viewModel;

    private boolean isFirstLogin = false;

    public SetSecretFragment() {
    }

    public void setFirstLogin(boolean firstLogin) {
        isFirstLogin = firstLogin;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentSetSecretBinding.inflate(inflater);
        viewModel = new ViewModelProvider(this, new ViewModelFactory())
                .get(LoginViewModel.class);
        initView();
        return binding.getRoot();
    }

    private void initView(){
        binding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBack();
            }
        });
        binding.btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String secret = binding.secret.getText().toString();
                String reSecret = binding.reSecret.getText().toString();
                if (StringUtils.isNotEmpty(secret)
                        && StringUtils.isNotEmpty(reSecret)
                        && secret.equals(reSecret)){
                    if (!checkPwd(secret)){
                        return;
                    }
                    viewModel.userPwdModify(getContext(), secret);
                }else {
                    showToast("请输入密码并保持一致");
                }
            }
        });

        viewModel.getUserInfo().observe(getViewLifecycleOwner(), new Observer<UserInfoEntity>() {
            @Override
            public void onChanged(UserInfoEntity userInfoEntity) {
                if (ObjectUtils.isNotEmpty(userInfoEntity)){
                    showToast("密码设置成功");
                    if (isFirstLogin){
                        isFirstLogin = false;
                        finish();
                    }else {
                        isFirstLogin = false;
                        popPage(LOGIN_SECRET_FRAGMENT);
                    }
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        clean();
    }

    private void clean(){
        viewModel.getError().setValue(null);
        binding.secret.setText("");
        binding.reSecret.setText("");
        viewModel.getUserInfo().setValue(null);
    }

    @Override
    public void showToast(String txt) {
        Toast.makeText(
                getContext(),
                txt,
                Toast.LENGTH_LONG).show();
    }
}